<template>
  <div >
    <div class="zan-cell zan-field">
      <div class="zan-cell__hd zan-field__title">卡号</div>
      <input type="text"
        v-model="form.card_no"
        placeholder="请输入会员卡号"
        class="zan-field__input zan-cell__bd"/>
    </div>
    <div class="zan-cell zan-field">
      <div class="zan-cell__hd zan-field__title">旧密码</div>
      <input type="password"
        v-model="form.old_passwd"
        placeholder="请输入会员密码"
        class="zan-field__input zan-cell__bd"/>
    </div>
    <div class="zan-cell zan-field">
      <div class="zan-cell__hd zan-field__title">新密码</div>
      <input type="password"
        v-model="form.password"
        placeholder="请输入会员新密码"
        class="zan-field__input zan-cell__bd"/>
    </div>
    <div class="zan-cell zan-field">
      <div class="zan-cell__hd zan-field__title">再确认</div>
      <input type="password"
        v-model="form.checkPassword"
        placeholder="请输入会员新密码"
        class="zan-field__input zan-cell__bd"/>
    </div>
    <div class="zan-cell zan-field">
      <div class="btn" @click="submit">确认</div>
    </div>
  </div>
</template>

<script>
import { getComponentByTag } from '@/utils/helper'
import { mapGetters } from 'vuex'
import api from '@/utils/api'
export default {
  data () {
    return {
      form:{
        card_no:'',
        password:'',
        old_passwd:'',
        checkPassword:''
      }
    }
  },
  computed: {
    ...mapGetters([
      'login_info',
    ])
  },
  beforeMount() {
    console.log('beforeMount')
    if (!this.login_info || !this.login_info.brand_id) {
      // 用户未授权
      let url = 'get_user_info?callback_url=modify_password'
      wx.redirectTo({
        url: url
      })
      return
    }
  },
  methods: {
    submit(){
      if (this.form.password != this.form.checkPassword) {
        wx.showToast({icon:'none',title:'确认新密码错误！'})
        return
      }
      let submitForm = {
        brand_id: this.login_info.brand_id,
        card_no: this.form.card_no,
        password: this.form.password,
        old_passwd:this.form.old_passwd
      }
      api.updatePasswd(submitForm).then(result=>{
        if(result.code == 0){
          wx.showToast({icon:'none',title:'修改密码成功！'})
          setTimeout(function () {
            this.form = {
              card_no:'',
              password:'',
              old_passwd:'',
              checkPassword:''
            }
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      })
    }
  }
}
</script>

<style >
.btn{
  height: 40px;
  line-height: 40px;
  width: 80%;
  color: white;
  text-align: center;
  background: black;
  border-radius: 50px;
  margin: 5px auto ;
}
</style>
